package tests;

import va.Range;

public class RangeTest {

	public static void main(String[] args) {
		testConstructor();
		testEqualRanges();
		testNonEqualRanges();
		testNonEqualRanges2();
		testRangeIndexEven();
		testRangeIndexOdd();
		testRangeNotExist();
	}
	
	public static void testConstructor() {
		try {
			int upper = 2;
			int lower = 0;
			Range range = new Range(lower, upper);
			
			if (range.lowerBound != lower) throw new Exception("Failed asserting lower bound");
			if (range.upperBound != upper) throw new Exception("Failed asserting upper bound");
			
			System.out.println("Passed testConstructor");
		} catch (Exception e) {
			System.out.println("testConstructor failed: " + e.getMessage());
		}
	}
	
	public static void testEqualRanges() {
		try {
			Range[] correctRanges = new Range[] { new Range(0, 24), new Range(25, 49), new Range(50, 74), new Range(75, 99) };
			Range[] actualRanges = Range.DefineRanges(2, 0, 99);
			assertRanges(correctRanges, actualRanges);
			System.out.println("Passed testEqualRanges");
		} catch (Exception e) {
			System.out.println("testEqualRanges failed: " + e.getMessage());
		}
	}
	
	public static void testNonEqualRanges() {
		try {
			Range[] correctRanges = new Range[] { new Range(0, 25), new Range(26, 50), new Range(51, 75), new Range(76, 100) };
			Range[] actualRanges = Range.DefineRanges(2, 0, 100);
			assertRanges(correctRanges, actualRanges);
			System.out.println("Passed testNonEqualRanges");
		} catch (Exception e) {
			System.out.println("testNonEqualRanges failed: " + e.getMessage());
		}
	}
	
	public static void testNonEqualRanges2() {
		try {
			Range[] correctRanges = new Range[] { new Range(0, 25), new Range(26, 51), new Range(52, 77), new Range(78, 102) };
			Range[] actualRanges = Range.DefineRanges(2, 0, 102);
			assertRanges(correctRanges, actualRanges);
			System.out.println("Passed testNonEqualRanges2");
		} catch (Exception e) {
			System.out.println("testNonEqualRanges2 failed: " + e.getMessage());
		}
	}
	
	public static void testRangeIndexEven() {
		try {
			Range[] ranges = new Range[] { new Range(0, 25), new Range(26, 51), new Range(52, 77), new Range(78, 102) };
			int index = Range.FindRangeIndex(ranges, 0);
			assertRangeIndex(0, index);
			index = Range.FindRangeIndex(ranges, 10);
			assertRangeIndex(0, index);
			index = Range.FindRangeIndex(ranges, 25);
			assertRangeIndex(0, index);
			index = Range.FindRangeIndex(ranges, 26);
			assertRangeIndex(1, index);
			index = Range.FindRangeIndex(ranges, 52);
			assertRangeIndex(2, index);
			index = Range.FindRangeIndex(ranges, 78);
			assertRangeIndex(3, index);
			System.out.println("Passed testRangeIndexEven");
		} catch (Exception e) {
			System.out.println("testRangeIndexEven failed: " + e.getMessage());
		}
	}
	
	public static void testRangeIndexOdd() {
		try {
			Range[] ranges = new Range[] { new Range(0, 25), new Range(26, 51), new Range(52, 77) };
			int index = Range.FindRangeIndex(ranges, 0);
			assertRangeIndex(0, index);
			index = Range.FindRangeIndex(ranges, 10);
			assertRangeIndex(0, index);
			index = Range.FindRangeIndex(ranges, 25);
			assertRangeIndex(0, index);
			index = Range.FindRangeIndex(ranges, 26);
			assertRangeIndex(1, index);
			index = Range.FindRangeIndex(ranges, 52);
			assertRangeIndex(2, index);
			System.out.println("Passed testRangeIndexOdd");
		} catch (Exception e) {
			System.out.println("testRangeIndexOdd failed: " + e.getMessage());
		}
	}
	
	public static void testRangeNotExist() {
		try {
			Range[] ranges = new Range[] { new Range(0, 25), new Range(26, 51), new Range(52, 77) };
			int index = Range.FindRangeIndex(ranges, 100);
			assertRangeIndex(-1, index);
			System.out.println("Passed testRangeNotExist");
		} catch (Exception e) {
			System.out.println("testRangeNotExist failed: " + e.getMessage());
		}
	}
	
	public static void assertRanges(Range[] correctRanges, Range[] actualRanges) throws Exception {
		if (correctRanges.length != actualRanges.length) throw new Exception("Length of arrays is incorrect");
		for (int i = 0; i < correctRanges.length; i++) {
			Range correctRange = correctRanges[i];
			Range actualRange = actualRanges[i];
			
			if (correctRange.lowerBound != actualRange.lowerBound) throw new Exception("Lower bound is " + actualRange.lowerBound + " but should be " + correctRange.lowerBound);
			if (correctRange.upperBound != actualRange.upperBound) throw new Exception("Upper bound is " + actualRange.upperBound + " but should be " + correctRange.upperBound);
		}
	}
	
	public static void assertRangeIndex(int correctIndex, int calculatedIndex) throws Exception {
		if (correctIndex != calculatedIndex) {
			throw new Exception("The correct index is: " + correctIndex + " but the calculated index was: " + calculatedIndex);
		}
	}

}
